﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AwesomeEnginePC.Graphics.TwoDimensional.Sprites;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AwesomeEnginePC.Graphics.TwoDimensional.Level
{
    public abstract class BaseMappyLevel : Base2DObject
    {
        protected int _tileWidth;
        protected int _tileHeight;

        protected ContentManager cm;

        protected SpriteSheet SpriteSheet { get; set; }
        protected Dictionary<int, int[,]> layerData { get; set; }

        public void SetLayerData(int index, int[,] dataForLayer)
        {
            layerData.Add(index, dataForLayer);
        }

        public BaseMappyLevel(ContentManager contentManager, string spriteSheetAssetName, int tileWidth, int tileHeight)
            : base(contentManager)
        {
            cm = contentManager;
            SpriteSheet = new SpriteSheet(cm, spriteSheetAssetName, tileWidth, tileHeight);
            _tileWidth = tileWidth;
            _tileHeight = tileHeight;

            layerData = new Dictionary<int, int[,]>();
        }

        protected override void DrawInner(GameTime gameTime, SpriteBatch sb)
        {
            //draw each layer in order from its index value
            foreach (var layer in layerData.OrderBy((x) => x.Key))
            {
                int horizontalDataPoints = layer.Value.GetLength(0);
                int verticalDataPoints = layer.Value.Length;

                for (int y = 0; y < verticalDataPoints; y++)
                {
                    for (int x = 0; x < horizontalDataPoints; x++)
                    {
                        SpriteSheet.ChangeFrame(layer.Value[y, x]);
                        SpriteSheet.Position.X = this.Position.X + (x * _tileWidth);
                        SpriteSheet.Position.Y = this.Position.Y + (y * _tileHeight);

                        SpriteSheet.Draw(gameTime, sb);
                    }
                }
            }
        }
    }
}
